import {Component, Input, OnInit} from '@angular/core';
import {HttpClient} from '@angular/common/http';
import { Output, EventEmitter } from '@angular/core';
@Component({
  selector: 'app-course-modal',
  templateUrl: './course-modal.component.html',
  styleUrls: ['./course-modal.component.scss']
})
export class CourseModalComponent implements OnInit {
  // 输出点击事件新增修改成功调用 父组件初始化数据方法 this.initData.emit();
  @Output() initData = new EventEmitter<string>();
  // 表单对象
  @Input() courseForm: any;
  // 表单类型 1.新增 2.修改
  @Input() courseType: any;
  // temp
  @Input() modal: any;
  // @Input() initData: any;
  // 初始化课程列表
  public courseList: any;
  constructor(private http: HttpClient) { }

  ngOnInit(): void {
  }
  /*
  * 保存课程
  * */
  saveWork(): void{
    if (this.courseType === 1){ // 新增
      this.http.post('/api/addCourse', this.courseForm.value, {headers: {'Content-Type': 'application/json'}})
        .subscribe((res: any) => {
          if (res) {
            this.initData.emit();
            this.modal.dismiss('Cross click');
          }
        });
    } else{ // 修改
      this.http.post('/api/updateCourse', this.courseForm.value, {headers: {'Content-Type': 'application/json'}})
        .subscribe((res: any) => {
          if (res) {
            this.initData.emit();
            this.modal.dismiss('Cross click');
          }
        });
    }
  }
}
